package controllers

import (
	"github.com/astaxie/beego"
	"github.com/astaxie/beego/orm"
	"strings"
	"time"
)

// TODO
// Users can customize the function processing flow in this file.

var driverName string

func init() {
	// set initialization here

	if beego.AppConfig.DefaultString("oracle.switch", "off") == "on" {
		driverName = "oracle"
	}

	if beego.AppConfig.DefaultString("mysql.switch", "off") == "on" {
		driverName = "mysql"
	}
}

func isWorkDay() bool {
	// set rule of workday here
	week := time.Now().Weekday()

	if week != time.Saturday && week != time.Sunday {
		return true
	}

	return false
}

func isDependCustomOk(dependCustom string) bool {
	// set depend check here

	if len(driverName) == 0 {
		return true
	}

	dependCustom = strings.Replace(dependCustom, "{{today}}", "'"+time.Now().Format("2006-01-02")+"'", -1)
	dependCustom = strings.Replace(dependCustom, "{{yesterday}}", "'"+time.Now().AddDate(0, 0, -1).Format("2006-01-02")+"'", -1)

	o := orm.NewOrm()

	o.Using(driverName)

	var value int64

	if err := o.Raw(dependCustom).QueryRow(&value); err != nil {
		beego.Error(err)
		return false
	}

	if value > 0 {
		return true
	} else {
		return false
	}
}

func setMonitor(jobDtl *TblJobDtl) {
	// trigger monitor here, print log or send request

	_ = jobDtl
}
